﻿using System;
using System.Collections.Generic;
using System.Text;
using EM.OA.DataModel.DBModel;
using EM.FrameWork.Paging;
using EM.OA.DAL;
using System.Linq;

namespace EM.OA.BLL
{
    public class WFApplyBLL : BLLBase<EMOA_WF_Apply>
    {
        public PagedList<EMOA_WF_Apply> GetApplyList(EMOA_WF_Apply model, PageEntity pageEntity)
        {
            using (var db = new EMOAContext())
            {
                var list = from q in
                               (
                                   from p in db.EMOA_WF_Apply
                                   select new
                                   {
                                       p,
                                       ApplyUserName = (from u in db.EMOA_User where u.UserID == p.ApplyUserID select u.EmployeeName).FirstOrDefault(),
                                       NextApprovalUserName = (from u in db.EMOA_User where u.UserID == ((from r in db.EMOA_WF_Approval where r.ApplyID == p.ApplyID orderby r.CreateTime descending select r.ApprovalUserID).FirstOrDefault()) select u.EmployeeName).FirstOrDefault(),//该申请现在的审批人
                                       NextApprovalUserID = (from r in db.EMOA_WF_Approval where r.ApplyID == p.ApplyID orderby r.CreateTime descending select r.ApprovalUserID).FirstOrDefault(),
                                       ApplyDepartmentName = (from d in db.EMOA_Department where d.DepartmentID == p.ApplyDepartmentID select d.DepartmentName).FirstOrDefault(),
                                       ApplyFlowConfigName = (from f in db.EMOA_WF_FlowConfig where f.FlowConfigID == p.ApplyFlowConfigID select f.FlowConfigName).FirstOrDefault(),
                                       FlowKind = (from config in db.EMOA_WF_FlowConfig where config.FlowConfigID == p.ApplyFlowConfigID select config.FlowKind).FirstOrDefault(),
                                       FlowKindName = (from dic in db.EMOA_Dictionary
                                                       where dic.DictionaryID == (
                                                           from config in db.EMOA_WF_FlowConfig where config.FlowConfigID == p.ApplyFlowConfigID select config.FlowKind
                                                           ).FirstOrDefault()
                                                       select dic.DictionaryName).FirstOrDefault(),
                                       NextFlowConfigDetailID = (from a in db.EMOA_WF_FlowConfigDetail
                                                                 where a.FlowConfigID == p.ApplyFlowConfigID &&
                                                                 a.StepLevel > (from b in db.EMOA_WF_FlowConfigDetail
                                                                                where b.FlowConfigDetailID == (from c in db.EMOA_WF_Approval
                                                                                                               where c.ApplyID == p.ApplyID
                                                                                                               orderby c.CreateTime descending
                                                                                                               select c.FlowConfigDetailID).FirstOrDefault()
                                                                                select b.StepLevel).FirstOrDefault()
                                                                 orderby a.StepLevel
                                                                 select a.FlowConfigDetailID).FirstOrDefault(),
                                       NowApprovalID = (from c in db.EMOA_WF_Approval
                                                        where c.ApplyID == p.ApplyID
                                                        orderby c.CreateTime descending
                                                        select c.ApprovalID).FirstOrDefault()
                                   })
                           where (!model.Status.HasValue || q.p.Status == model.Status) &&
                           (!model.FlowKind.HasValue || q.FlowKind == model.FlowKind) &&
                           (model.ApplyUserID == new Guid() || q.p.ApplyUserID == model.ApplyUserID) &&
                           (model.NextApprovalUserID == new Guid() || q.NextApprovalUserID == model.NextApprovalUserID)
                           orderby q.FlowKindName, q.ApplyFlowConfigName, q.ApplyDepartmentName, q.ApplyUserName
                           select q;



                return EM.FrameWork.Common.GenerateComplexObject<EMOA_WF_Apply>(list.ToList()).ToPagedList(pageEntity.PageIndex, pageEntity.PageSize);
            }
        }

    }
}